//
//  KZExperienceController.m
//  xiaorizi
//
//  Created by kun on 15/10/26.
//  Copyright © 2015年 origin. All rights reserved.
//

#import "KZExperienceController.h"
#import "KZGlobalApi.h"
#import "experienceHeaderModel.h"
#import "ExperienceEventsModel.h"
#import "KZExperienceHeaderView.h"

#import "KZExperienceCell.h"
#import "KZExperienceEventsController.h"

@interface KZExperienceController ()
@property (nonatomic,strong) NSMutableArray *headerData;
@property (nonatomic,strong) KZExperienceHeaderView *headerView;

@end

@implementation KZExperienceController

- (void)viewDidLoad {
    self.url = [KZGlobalApi experienceUrl];
    self.cachePath = [KZGlobalApi experienceUrlCachePath];
    self.refreshType = FLRefreshTypeHeader;
    
    self.headerData = [NSMutableArray array];
    
    [super viewDidLoad];
    
    //添加头部视图
#warning 头部界面跳转有问题？
    KZExperienceHeaderView *headerView = [KZExperienceHeaderView experienceTableHeaderView];
    _headerView = headerView;
    self.headerView.dataArray = self.headerData;
    self.tableView.tableHeaderView = headerView;
    headerView.backgroundColor = [UIColor redColor];
    __weak KZExperienceController *weakSelf = self;
    [weakSelf.headerView setClickedExperienceHeaderBlock:^(KZExperienceHeaderView * headView, experienceHeaderModel * model) {
        //界面跳转
//        KZExperienceHeaderViewController *view = [[KZExperienceHeaderViewController alloc]init];
//        [weakSelf.navigationController pushViewController: view animated:YES];
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//重写父类方法

-(NSString *)JsonModelPath
{
    return [[NSBundle mainBundle]pathForResource:@"tiyan" ofType:@"json"];
}


- (id)createDataWithNode:(id)node
{
    ExperienceEventsModel *model = [ExperienceEventsModel initWithDic:node];
    return model;
}

-(NSArray *)createDataArray:(NSDictionary *)dic
{
    //1:解析头部数据
    NSArray *headerData = dic[@"head"];

    [self.headerData removeAllObjects];
    
    for(NSDictionary *dic in headerData)
    {
        experienceHeaderModel *model = [experienceHeaderModel initWithDic:dic];
        [self.headerData addObject:model];
    }
    self.headerView.dataArray = self.headerData;
    
    NSArray *arrayData = dic[@"list"];
    
    return arrayData;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KZExperienceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"experienceCell" forIndexPath:indexPath];

    ExperienceEventsModel *model = self.data[indexPath.row];
    
    //block回调
    [cell setExperienceImgViewBlock:^(UITableViewCell * cell, ExperienceEventsModel * data) {
        
        self.hidesBottomBarWhenPushed = YES;
        [self performSegueWithIdentifier:@"showExperienceEvents" sender:data];
        self.hidesBottomBarWhenPushed = NO;
    }];
    
    
    cell.model = model;
    return cell;
}


//
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//
//    return headerView;
//}



//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    // return 500;
//    //1 cell 的高度实现
//    CGFloat appvieww= ([UIScreen mainScreen].bounds.size.width - 16)/3;
//    CGFloat appviewh= appvieww*0.8;
//    
//    CategoryModel *model = self.data[indexPath.row];
//    CGFloat H = (appviewh * model.tags.count/3) + 70;
//    return H;
//}



#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  
    if([segue.identifier isEqualToString:@"showExperienceEvents"])
    {
        KZExperienceEventsController *headView = segue.destinationViewController;
        headView.model = (ExperienceEventsModel *)sender;
        
    }
    
}


@end
